#include <myheader_c_plus.h>
using std::endl;
using std::cout;
class myString{
public:
    //构造函数
    myString()
    :_pstr(new char[1]())
    {

    }


    //构造函数
    myString(const char * pstr)
    :_pstr(new char[strlen(pstr) + 1]())
    {
        cout << "myString(const char * pstr)" << endl;
        strcpy(_pstr, pstr);
    }

    //拷贝构造函数
    myString(const myString & rhs)
    :_pstr(new char[strlen(rhs._pstr) + 1]())
    {
        cout << "myString(const myString & rhs)" << endl;
        strcpy(_pstr, rhs._pstr);
    }

    //移动构造函数
    myString(myString && rhs)
    :_pstr(rhs._pstr)
    {
        cout << "myString(myString && rhs)" << endl;
        rhs._pstr = nullptr;
    }

    //移动赋值函数
    myString & operator=(myString && rhs){
      //  if(this != &rhs){
            delete [] _pstr;
            _pstr = rhs._pstr;
            rhs._pstr = nullptr;
       // }
        cout << "myString operator=(myString &&)" << endl;
        return *this;
    }

    void getString(){
        cout << _pstr << endl;
    }


private:
    char * _pstr;
};

void test(){
    myString s("hello");//左值
    s = std::move(s);//自赋值，转换成右值
    s.getString();
}

int && func(int a, int b){
    return std::move(a + b);
}
int main(int argc, char *argv[])
{
    myString s1("hello");
    myString s2("world");
    s2 = myString("nihao");
    s2.getString();
    myString s3 = "hello";
    s3.getString();
    cout << "============" << endl;
    //test();
    int && ref = func(1, 2);
    &ref;
    return 0;
}

